import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:todo_list/config/all_types.dart';
import 'package:todo_list/config/api_service.dart';
import 'package:todo_list/config/provider_config.dart';
import 'package:todo_list/model/all_model.dart';
import 'package:todo_list/utils/shared_util.dart';

class AccountPageLogic {
  final AccountPageModel _model;

  AccountPageLogic(this._model);

  Future getAvatarUrl() async {
    _model.avatarUrl =
        await SharedUtil.instance.getString(Keys.localAvatarPath);
  }

  Future getUserName() async {
    _model.userName = await SharedUtil.instance.getString(Keys.currentUserName);
  }

  Future getContri() async {
    _model.contri = await SharedUtil.instance.getInt("contri");
  }
  void onIncScore() async {
    _model.contri = await SharedUtil.instance.getInt("contri");
    final String id = await SharedUtil.instance.getString("id");
    ApiService.instance.incContri(
        params: {'_id': id},
        success: (InfoUpdateBean bean) {
          if (bean.contri == 0) {
            _showTextDialog("您今天已经签到了，请明天再来!");
          } else {
            _showTextDialog("签到成功，现在积分为" + bean.contri.toString());
            _model.contri = bean.contri;
            _model.refresh();
            SharedUtil.instance.saveInt("contri", bean.contri);
          }
        },
        failed: () => {debugPrint("failed")});



  }
  void _showTextDialog(String text) {
    final context = _model.context;
    showDialog(
        context: context,
        builder: (ctx) {
          return AlertDialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(20.0))),
            content: Text(text),
          );
        });
  }

  Future getIc() async {
    _model.invitecode = await SharedUtil.instance.getString("invitecode");
  }

  Future getMyic() async {
    _model.myic = await SharedUtil.instance.getString("myic");
  }

  Future getCoin() async {
    _model.coin = await SharedUtil.instance.getInt("coin");
  }

  Future getVip() async {
    _model.vip = await SharedUtil.instance.getInt("vip");
  }
  Future getSubCounts() async {
    _model.subCounts = await SharedUtil.instance.getInt("subCounts");
  }
  Future getEmailAccount() async {
    _model.emailAccount = await SharedUtil.instance.getString(Keys.account);
  }

  Future getBackgroundType() async {
    _model.backgroundType = await SharedUtil.instance
            .getString(Keys.currentAccountBackgroundType) ??
        AccountBGType.defaultType;
  }

  Future getBackgroundUrl() async {
    _model.backgroundUrl =
        await SharedUtil.instance.getString(Keys.currentAccountBackground);
  }

  Widget getAvatar(Color color) {
    return _model.avatarUrl != null
        ? Container(
            width: 100,
            height: 100,
            child: ClipRRect(
              child: Image.file(File(_model.avatarUrl)),
              borderRadius: BorderRadius.all(Radius.circular(50)),
            ),
          )
        : Container(
            width: 100,
            height: 100,
            child: ClipRRect(
              child: CircularProgressIndicator(
                valueColor: AlwaysStoppedAnimation(color),
              ),
              borderRadius: BorderRadius.all(Radius.circular(50)),
            ),
          );
  }

  void onLogoutPressed() {
    SharedUtil.instance.saveString(Keys.account, "default").then((v) {
      SharedUtil.instance.saveBoolean(Keys.hasLogged, false);
    });

    Navigator.of(_model.context).pushAndRemoveUntil(
        new MaterialPageRoute(builder: (context) {
      return ProviderConfig.getInstance().getLoginPage(isFirst: true);
    }), (router) => router == null);
  }

  void _showDialog(String text, BuildContext context) {
    showDialog(
        context: context,
        builder: (ctx) {
          return AlertDialog(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(
                Radius.circular(20),
              ),
            ),
            content: Text(text),
          );
        });
  }

  void onResetPasswordPressed() {
    _showDialog("修改密码未开放", _model.context);

    // Navigator.of(_model.context).push(new CupertinoPageRoute(builder: (ctx) {
    //   return ProviderConfig.getInstance().getResetPasswordPage();
    // }));
  }

  void onBackgroundTap() {
    Navigator.of(_model.context).push(new CupertinoPageRoute(builder: (ctx) {
      return ProviderConfig.getInstance().getNetPicturesPage(
        useType: NetPicturesUseType.accountBackground,
        accountPageModel: _model,
      );
    }));
  }
}
